using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace MapEditor
{
    static class Movement
    {

        public static  bool MoveTo(ref double _X, ref double _Y, Point TargetLocation, double MaxDistance, ref double distanceMoved)
            //update a unit's location by moving it its max allowable distance towards the target
        {
            //currently only accurate for non subpixel movement

            dPoint InitialLocation = new dPoint(_X, _Y);


            //get the angle to the destination
            double TargetAngle = MathUtils.GetAngle(InitialLocation.ToPoint(), TargetLocation);
            //get the distance to the target
            double TargetDistance = MathUtils.GetDistance(InitialLocation.ToPoint(), TargetLocation);
            
            //if the distance we can move is less than the distance to the target, move there
            if (TargetDistance < MaxDistance)
            {
                _X = TargetLocation.X;
                _Y = TargetLocation.Y;
                distanceMoved = TargetDistance;
                return true;
            }

            //otherwise, we can only move part of the way there
            double xOffset =  Math.Abs((Math.Cos((TargetAngle*(Math.PI/180))))) * MaxDistance;
            double yOffset = Math.Abs((Math.Sin((TargetAngle*(Math.PI/180)))))* MaxDistance;
            if (_X < TargetLocation.X)
            {
                _X += xOffset;
                if (_X > TargetLocation.X)
                {
                    _X = TargetLocation.X;
                }
            }
            else
            {
                _X -= xOffset;
                if (_X < TargetLocation.X)
                {
                    _X = TargetLocation.X;
                }
            }
            if (_Y < TargetLocation.Y)
            {
                _Y += yOffset;
                if (_Y > TargetLocation.Y)
                {
                    _Y = TargetLocation.Y;
                }
            }
            else
            {
                _Y -= yOffset;
                if (_Y < TargetLocation.Y)
                {
                    _Y = TargetLocation.Y;
                }
            }

            distanceMoved = MaxDistance;
            return false;

        }

        public static bool TurnTo(ref double _Facing, double NeedAngle, double MaxTurn)
        {

            if (Math.Abs(NeedAngle - _Facing) > 180)
            {
                if (_Facing < NeedAngle)
                { _Facing = 360; }
                else
                { NeedAngle += 360; }
            }

            if (NeedAngle == _Facing)
            {
                return true;
            }

            if (NeedAngle > _Facing)
            {

                _Facing += MaxTurn;
                if (_Facing > NeedAngle)
                {
                    _Facing = NeedAngle;
                    return true;
                }
            }

            if (NeedAngle < _Facing)
            {
                _Facing -= MaxTurn;
                if (_Facing < NeedAngle)
                {
                    _Facing = NeedAngle;
                    return true;
                }
            }

            return false;
        }
    }
}
